﻿Imports TMySQL
Public Class frmColumnPrivileges
    Private _PrivList As TMyColumnPrivilegesList
    Private Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub
    Public Sub New(ByVal APrivList As TMyColumnPrivilegesList)
        Me.New()
        _PrivList = APrivList
        InitFrm()
    End Sub
    Private Sub InitFrm()
        lstDatabase.Items.Clear()
        For i As Integer = 0 To _PrivList.Count - 1
            If Not lstDatabase.Items.Contains(_PrivList(i).Db) Then
                lstDatabase.Items.Add(_PrivList(i).Db)
            End If
        Next
    End Sub

    Private Sub lstDatabase_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDatabase.SelectedIndexChanged
        Cursor.Current = Cursors.WaitCursor
        lvTables.SuspendLayout()
        lvColumns.SuspendLayout()
        Try
            InitCheck()
            lvTables.Items.Clear()
            lvColumns.Items.Clear()
            If lstDatabase.SelectedItems.Count <= 0 Then
                Return
            End If
            ShowTables(lstDatabase.SelectedItem)
        Finally
            lvTables.ResumeLayout()
            lvColumns.ResumeLayout()
            Cursor.Current = Cursors.Default
        End Try

    End Sub

    Private Sub ShowTables(ByVal ADatabaseName As String)
        lvTables.Items.Clear()
        lvColumns.Items.Clear()
        For i As Integer = 0 To _PrivList.Count - 1
            If _PrivList(i).Db.Equals(ADatabaseName) Then
                If Not TableExists(_PrivList(i).TableName) Then
                    Dim li As New ListViewItem
                    li.Text = _PrivList(i).TableName
                    li.SubItems.Add(_PrivList(i).Engine)
                    li.SubItems.Add(Common.DateTimeToString(_PrivList(i).CreateTime))
                    li.SubItems.Add(Common.DateTimeToString(_PrivList(i).UpdateTime))
                    li.SubItems.Add(_PrivList(i).TableComment)
                    li.Tag = _PrivList(i).Db
                    lvTables.Items.Add(li)
                End If
            End If
        Next
        For Each col As ColumnHeader In lvTables.Columns
            col.Width = -2
        Next
    End Sub
    Private Function TableExists(ByVal ATableName As String) As Boolean
        For Each item As ListViewItem In lvTables.Items
            If ATableName.Equals(item.Text) Then
                Return True
            End If
        Next
        Return False
    End Function
    Private Sub lvTables_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvTables.ItemSelectionChanged
        Dim dbName As String = e.Item.Tag
        Dim tabName As String = e.Item.Text

        Cursor.Current = Cursors.WaitCursor
        lvColumns.SuspendLayout()
        Try
            InitCheck()
            If Not e.IsSelected Then
                Return
            End If
            lvColumns.Items.Clear()
            ShowColumns(dbName, tabName)
        Finally
            lvColumns.ResumeLayout()
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub ShowColumns(ByVal ADatabaseName As String, ByVal ATableName As String)
        lvColumns.Items.Clear()
        For i As Integer = 0 To _PrivList.Count - 1
            If _PrivList(i).Db.Equals(ADatabaseName) AndAlso _PrivList(i).TableName.Equals(ATableName) Then
                Dim li As New ListViewItem
                li.Text = _PrivList(i).ColumnName
                li.SubItems.Add(_PrivList(i).ColumnType)
                li.SubItems.Add(_PrivList(i).ColumnComment)
                li.Tag = _PrivList(i)
                lvColumns.Items.Add(li)
            End If
        Next
        For Each col As ColumnHeader In lvColumns.Columns
            col.Width = -2
        Next
    End Sub

    Private Sub lvColumns_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvColumns.ItemSelectionChanged
        Cursor.Current = Cursors.WaitCursor
        Try
            InitCheck()
            If Not e.IsSelected Then
                Return
            End If
            ShowPrivs(e.Item.Tag)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub InitCheck()
        chkSelect.Checked = False
        chkInsert.Checked = False
        chkUpdate.Checked = False
        chkReferences.Checked = False
    End Sub

    Private Sub ShowPrivs(ByVal APriv As TMyColumnPrivileges)
        chkSelect.Checked = APriv.SelectPriv
        chkInsert.Checked = APriv.InsertPriv
        chkUpdate.Checked = APriv.UpdatePriv
        chkReferences.Checked = APriv.ReferencesPriv
    End Sub
End Class